<template>
	<view>
		<uni-nav-bar left-icon="back" left-text="返回" fixed="true" statusBar="true"  @clickLeft="leftAction" :title="clientId+'配置'"></uni-nav-bar>

		<view class="content">
			<form @submit="formSubmit">
				<view class="uni-form-item uni-row">
					<view class="title">终端LORA发射功率(TP)</view>
					<uni-data-picker class="uni-picker" v-model="client.TP" :localdata="tp_arr" popup-title="请选择终端LORA发射功率"
						@change="onchangeTP" @nodeclick="onnodeclick"></uni-data-picker>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">终端空中唤醒配置(DC)</view>
					<uni-data-picker class="uni-picker" v-model="client.DC" :localdata="dc_arr" popup-title="请选择终端空中唤醒配置"
						@change="onchangeDC" @nodeclick="onnodeclick"></uni-data-picker>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">终端采样时间间隔(SP)</view>
					<uni-data-picker class="uni-picker" v-model="client.SP" :localdata="sp_arr" popup-title="请选择终端采样时间间隔"
						@change="onchangeSP" @nodeclick="onnodeclick"></uni-data-picker>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">终端上传数据时间间隔(UT)</view>
					<uni-data-picker class="uni-picker" v-model="client.UT" :localdata="ut_arr" popup-title="请选择终端上传数据时间间隔"
						@change="onchangeUT" @nodeclick="onnodeclick"></uni-data-picker>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">终端报警阈值下限(LT)</view>
					<input class="uni-input" name="LT" v-model="client.LT" placeholder="请输入终端报警阈值下限" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">终端报警阈值上限(HT)</view>
					<input class="uni-input" name="HT" v-model="client.HT" placeholder="请输入终端报警阈值上限" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">终端测量校准值(CV)</view>
					<input class="uni-input" name="CV" v-model="client.CV" placeholder="终端测量校准值" />
				</view>
				<view class="uni-btn-v">
					<button form-type="submit">保存</button>
				</view>
			</form>
		</view>

	</view>
</template>

<script>
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue'
	import Vue from 'vue'
	import wsconnect from '@/api/webservice.js'

	export default {



		data() {
			return {
				tp_arr: [{
						text: "0dBm",
						value: "1"
					},
					{
						text: "2dBm",
						value: "2"
					},
					{
						text: "5dBm",
						value: "3"
					},
					{
						text: "7dBm",
						value: "4"
					},
					{
						text: "10dBm",
						value: "5"
					},
					{
						text: "14dBm",
						value: "6"
					},
					{
						text: "17dBm",
						value: "7"
					},
					{
						text: "22dBm",
						value: "8"
					}
				],
				dc_arr: [{
						text: "休眠2s/接收30ms",
						value: "1"
					},
					{
						text: "休眠2s/接收40ms",
						value: "2"
					},
					{
						text: "休眠3s/接收40ms",
						value: "3"
					},
					{
						text: "休眠4s/接收40ms",
						value: "4"
					},
					{
						text: "休眠5s/接收40ms",
						value: "5"
					},
				],
				sp_arr: [{
						text: '1s',
						value: "1"
					},
					{
						text: '5s',
						value: "2"
					},
					{
						text: '10s',
						value: "3"
					},
					{
						text: '30s',
						value: "4"
					},
					{
						text: '1min',
						value: "5"
					},
					{
						text: '3min',
						value: "6"
					},
					{
						text: '5min',
						value: "7"
					},
					{
						text: '10min',
						value: "8"
					},
					{
						text: '15min',
						value: "9"
					},
					{
						text: '30min',
						value: "10"
					},
					{
						text: '1h',
						value: "11"
					}
				],
				ut_arr: [{
						text: '1s',
						value: "1"
					},
					{
						text: '5s',
						value: "2"
					},
					{
						text: '30s',
						value: "3"
					},
					{
						text: '1min',
						value: "4"
					},
					{
						text: '3min',
						value: "5"
					},
					{
						text: '5min',
						value: "6"
					},
					{
						text: '10min',
						value: "7"
					},
					{
						text: '15min',
						value: "8"
					},
					{
						text: '30min',
						value: "9"
					},
					{
						text: '1h',
						value: "10"
					},
					{
						text: '2h',
						value: "11"
					},
				],
				client:{
					TP: "1",
					DC: "1",
					SP: "1",
					UT: "1",
					LT: "1",
					HT: "1",
					CV: "1",
				}
			};
		},
		onLoad(options) {
			this.clientId = options.clientId
			this.bs_id = options.id
			console.log(this.bs_id)
			this.$http.get('/netty/station/' + this.bs_id).then(res => {
				const form = res.data.data
				this.form = form
				this.getClientInfo()
			}).catch(err => {
				uni.showModal({
					content: '出错了，请稍后再试',
					showCancel: false
				})
			})
			uni.hideLoading()
		},
		components: {
			uniNavBar
		},
		methods: {
			leftAction() {
				uni.navigateBack({
					delta: 1
				});
			},
			onchangeTP(e) {
				this.client.TP = e.detail.value[0].value
			},
			onchangeDC(e) {
				this.client.DC = e.detail.value[0].value
			},
			onchangeSP(e) {
				this.client.SP = e.detail.value[0].value
			},
			onchangeUT(e) {
				this.client.UT = e.detail.value[0].value
			},
			onnodeclick(node) {
				console.log(node)
			},
			getClientInfo() {
				const sendCmdTagName = this.form.sendCmdTagName
				const reqTagName = this.form.reqTagName
				console.log(reqTagName)
				wsconnect.requestJson({
					items: [sendCmdTagName + '.DESC'],
					vals: ['TerInqRq,/TI='+this.clientId+'/'],
					type: "SetDataByName",
				}, reqTagName).then(res => {
					this.configDataList(res)
					wsconnect.clearReqData(1, reqTagName)
				}).catch(error => {
					if (error.code == 1) {
						uni.redirectTo({
							url: "../index/index"
						})
					} else {
						uni.showModal({
							content: error.msg,
							showCancel: false
						})
					}
				})
			},
			configDataList(res){
				const arr = res.split('/')
				const array = arr.slice(1, arr.length - 1)
				var dataDic = {}
				for (let i in array) {
					const result = array[i]
					console.log(result)
					const a = result.split('=')
					dataDic[a[0]] = a[1]
				}
				this.client = dataDic
			},
			formSubmit: function(e) {
				const sendCmdTagName = this.form.sendCmdTagName
				const reqTagName = this.form.reqTagName
				wsconnect.requestJson({
					items: [sendCmdTagName + '.DESC'],
					vals: ['TerConfigRq,/TI=' + this.clientId + '/TP=' + this.client.TP +  '/DC=' + this.client.DC + '/SP=' + this.client.SP + '/UT=' + this.client.UT+ '/LT=' + this.client.LT +'/HT=' + this.client.HT + '/CV=' + this.client.CV + '/'
					],
					type: "SetDataByName",
				}, reqTagName).then(res => {
					this.configDataList(res)
					wsconnect.clearReqData(1, reqTagName)
				}).catch(error => {
					if (error.code == 1) {
						uni.redirectTo({
							url: "../index/index"
						})
					} else {
						uni.showModal({
							content: error.msg,
							showCancel: false
						})
					}
				})
				
			}
		}
	}
</script>

<style lang="scss">

	@import '@/common/uni.css';
	@import '@/common/uni-nvue.scss';

	.uni-form-item .title {
		margin-left: 20rpx;
		padding: 20rpx 0;
		width: 370rpx;
		font-size: 30rpx;
	}
	
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
	
	.uni-btn-v {
		margin-left: 50rpx;
		margin-right: 50rpx;
		height: 40rpx;
	}
	
	.text-area {
		display: flex;
		justify-content: center;
	}
	
	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.uni-input {
		background-color: #F1F1F1;
		height: 60rpx;
		border: 1px solid #DDD;
		margin-right: 50rpx;
	}

	.uni-picker {
		background-color: #F1F1F1;
		height: 60rpx;
		margin-right: 50rpx;
	}
	
</style>
